package cloud.shyidong.core.exception;

import cloud.shyidong.core.constant.ErrorTypeConstant;

/**
 * @Description TODO
 * @Author syd
 * @Date 2023/3/27
 */
public final class BusinessException extends RuntimeException {
    
    private final int type;
    
    private final String message;
    
    private final Object data;
    
    
    public BusinessException(int type, String message, Object data) {
        this.type = type;
        this.message = message;
        this.data = data;
    }
    
    public BusinessException(int type, String message) {
        this.type = type;
        this.message = message;
        this.data = null;
    }
    
    public BusinessException(String message, Object data) {
        this(ErrorTypeConstant.BUSINESS_ERROR.getType(), message, data);
    }
    
    public BusinessException(String message) {
        this(ErrorTypeConstant.BUSINESS_ERROR.getType(), message);
    }
    
    public static BusinessException from(String message, Object data) {
        return new BusinessException(message, data);
    }
    
    public static BusinessException from(String message) {
        return new BusinessException(message);
    }
    
    public static BusinessException from(Exception e) {
        return new BusinessException(e.getMessage());
    }
    
    public static BusinessException noLogin() {
        return new BusinessException(ErrorTypeConstant.NO_LOGIN.getType(), null);
    }
    
    public static BusinessException noFunction() {
        return new BusinessException(ErrorTypeConstant.NO_FUNCTION.getType(), null);
    }
    
    public static BusinessException error() {
        return new BusinessException(ErrorTypeConstant.SYS_ERROR.getType(), null);
    }
    
    public int getType() {
        return type;
    }
    
    @Override
    public String getMessage() {
        return message;
    }
    
    public Object getData() {
        return data;
    }
    
    @Override
    public String toString() {
        return "BusinessException{" +
                "type=" + type +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}
